unit draw;
interface
uses graph, crt, constant;
  procedure OutRegistersValue;
  procedure DrawMashine;
  procedure Initialize;
  function WriteHexWord(w: word):  string;
implementation

procedure Initialize;
var
   GRDriver, GRMode,ErrCode: integer;
begin
  GRDriver:=Detect;
  GRMode:=1;
  InitGraph(GRDriver, GRMode, '');
  ErrCode:=GraphResult;
  if ErrCode <> grOk then
  begin
    WriteLn('Graphics Error:', GraphErrorMsg(ErrCode));
    Halt(1);
  end;
end;

procedure OutRegistersValue;
var 
  i: integer;
  st,st2: string[4];

begin
  for i:=1 to LimitRegistrs-1 do
  with Registrs[i] do
  begin
    str(value^,st);
    OutTextXY(x,y,'Hex='+WriteHexWord(value^));
    OutTextXY(x,y+dy,'Dec='+st);
  end;
  with Registrs[LimitRegistrs] do
  begin
    str(ZF,st);
    str(CF,st2);
    OutTextXY(x,y,'Bin='+st);
    OutTextXY(x+40,y,st2);
  end;
end;

procedure DrawMashine;
var 
  i: integer;
  st: string[4];
begin
  Rectangle(0,0,GetMaxX,GetMaxY);
  SetColor(lightBlue);
  for i:=1 to LimitDrawMemory do
  with DrawMemory[i] do
    Rectangle(x1,y1,x2,y2);
  for i:=1 to LimitDrawRegisters do
  with DrawRegisters[i] do
   if entity='square' 
   then
   begin
     SetColor(lightGreen);
     Rectangle(x1,y1,x2,y2)
   end
   else
   begin
     SetColor(lightBlue);
     Line(x1,y1,x2,y2);
   end;
  SetColor(lightGreen);
  for i:=1 to LimitTextMBM do
  with TextMBM[i] do
    OutTextXY(x+15,y,txt);
    SetColor(lightMagenta);
  for i:=12 to LimitDrawMemory do
  with DrawMemory[i] do
  begin
   if i=12
   then
      str(speedValue,st)
   else
      str(stepValue,st);
   OutTextXY(x1+15,y1+dy+5,'Dec='+st);
  end;
  OutRegistersValue;
  for i:=0 to LimitArrows do
  with Arrows[i] do
    if (i=1) or (i=0)
    then
      OutTextXY(x,y,'<')
    else
      OutTextXY(x,y,'>')
end;

function WriteHexWord(w: word):  string;
const
  HexChars : array [0..$F] of Char = '0123456789ABCDEF';
var 
  HexStr: string[4];
begin
  WriteHexWord:=HexChars[Hi(W) Shr 4]+HexChars[Hi(W) And $F]+HexChars[Lo(W) Shr 4]+        HexChars[Lo(W) And $F];
end;
begin
end.